package chapter.sixth.item30;

import java.util.Map;

import chapter.fifth.item27.GenericUtil;

public enum Operation {
	
	PLUS("+"){
		double apply(double x, double y) {
			return x+y;
		}
	},
	MINUS("-"){
		double apply(double x, double y) {
			return x-y;
		}
	},
	TIMES("*"){
		double apply(double x, double y) {
			return x*y;
		}
	},
	DEVICE("/"){
		double apply(double x, double y) {
			return x/y;
		}
	};
	
	private static final Map<String,Operation> stringToEnum =
			GenericUtil.newHashMap();
	
	static{
		for(Operation op:values())
			stringToEnum.put(op.symbol,op);
	}
	
	private final String symbol;
	Operation(String symbol){this.symbol = symbol;}
	
	@Override
	public String toString() {
		return symbol;
	}
	
	abstract double apply(double x,double y);
	
	public static Operation fromString(String symbol){
		return stringToEnum.get(symbol);
	}
	
	public static void main(String[] args) {
		
	}
}
